package com.tools.developer;

import java.lang.ref.SoftReference;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tools.developer.app.AppDetailActivity4Running;
import com.tools.developer.bean.DataBaseModel;

public class DataBaseActivity extends ListActivity {
	private String mPkgName;
	private static ArrayList<DataBaseModel> mDBList = new ArrayList<DataBaseModel>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_database);

		if (getIntent() != null) {
			mPkgName = getIntent().getStringExtra(AppDetailActivity4Running.PKG_NAME);
		}
		new DatabaseThread(new Handler()).start();
	}

	class DatabaseThread extends Thread {
		private Handler mHandler;

		DatabaseThread(Handler handler) {
			mHandler = handler;
		}

		@Override
		public void run() {

			mHandler.sendEmptyMessage(0);
		}
	}

	static class MyHandler extends Handler {
		private AppListAdapter mListAdapter;
		private ListView mListView;
		private View mWaitLayout;
		private SoftReference<Activity> mActivity;

		private MyHandler(Activity activity) {
			mActivity = new SoftReference<Activity>(activity);
			mListView = (ListView) activity.findViewById(android.R.id.list);
			mWaitLayout = activity.findViewById(R.id.database_loading_layout);
		}

		@Override
		public void handleMessage(Message msg) {
			if (mActivity != null) {
				if (mActivity.get() != null) {
					mListAdapter = new AppListAdapter(mActivity.get(), mDBList);
					mListView.setAdapter(mListAdapter);
					mWaitLayout.setVisibility(View.GONE);
					mListView.setVisibility(View.VISIBLE);
				}
			}
		}

	};

	private static class AppListAdapter extends BaseAdapter {
		private ArrayList<DataBaseModel> mDBList;
		private SoftReference<Activity> mActivity;

		public AppListAdapter(Activity activity, ArrayList<DataBaseModel> DBList) {
			mActivity = new SoftReference<Activity>(activity);
			mDBList = DBList;
		}

		@Override
		public int getCount() {
			return mDBList.size();
		}

		@Override
		public DataBaseModel getItem(int position) {
			return mDBList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (mActivity != null) {
				Activity activity = mActivity.get();
				if (activity != null) {
					ViewHolder holder = null;
					if (convertView == null) {
						convertView = activity
								.getLayoutInflater()
								.inflate(
										R.layout.app_list_item,
										null, false);
						holder = new ViewHolder();
						holder.iconView = (ImageView) convertView
								.findViewById(R.id.icon);
						holder.titleView = (TextView) convertView
								.findViewById(R.id.title);
						holder.pkgName = (TextView) convertView
								.findViewById(R.id.pkg_name);
						holder.ver = (TextView) convertView
								.findViewById(R.id.app_ver);
						holder.shareUid = (TextView) convertView
								.findViewById(R.id.app_share_uid);
						convertView.setTag(holder);

					} else {
						holder = (ViewHolder) convertView.getTag();
					}

					DataBaseModel app = getItem(position);
					PackageManager pm = activity.getPackageManager();
					try {
						holder.iconView.setImageDrawable(pm
								.getApplicationIcon(app.getPackageName()));
					} catch (NameNotFoundException e) {
					}
					holder.titleView.setText(app.getName());
					holder.pkgName.setText(app.getPackageName());
					holder.ver.setText(app.getmVer());
					holder.shareUid.setText(app.mShareUid);

				}
			}
			return convertView;
		}
	}

	private static class ViewHolder {
		ImageView iconView;
		TextView titleView;
		TextView pkgName;
		TextView ver;
		TextView shareUid;
	}
}
